package com.atguigu.config;

import com.atguigu.quartz.MyJob;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @author 张晓
 * @create 2020-12-03 22:15
 */
@Configuration
@ComponentScan("com.atguigu")
public class QuartzConfig {


    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public MethodInvokingJobDetailFactoryBean jobDetail(MyJob myJob) {
        System.out.println("MyJob...instance... " + myJob);
        MethodInvokingJobDetailFactoryBean jobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
        jobDetailFactoryBean.setTargetObject(myJob);
        jobDetailFactoryBean.setTargetMethod("doIt");
        return jobDetailFactoryBean;
    }

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public CronTriggerFactoryBean cronTrigger(MethodInvokingJobDetailFactoryBean jobDetail) {
        System.out.println("MethodInvokingJobDetailFactoryBean...instance... " + jobDetail);
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setJobDetail(jobDetail.getObject());
        cronTriggerFactoryBean.setCronExpression("*/5 * * * * ?");
        cronTriggerFactoryBean.setName("zx");
        return cronTriggerFactoryBean;
    }


    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(Trigger cronTrigger) {
        System.out.println("Trigger...instance... " + cronTrigger);
        SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
        scheduler.setTriggers(cronTrigger);
        return scheduler;
    }

}
